package com.molin.ArrayList;

import java.util.ArrayList;

public class Demo1AL {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("abc");
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");
        list.add("555");
        list.add("666");
        list.add("77");
//        testRemove(list);
//        testSet(list);
//        testGet(list);
//        System.out.println(list.size());
//        foriTest(list);
//        RemoveWord();
    }

    public static void foriTest(ArrayList<String> list) {
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));

        }
    }

    public static void testGet(ArrayList<String> list) {
        System.out.println(list.get(0));
        System.out.println(list.get(3));
        System.out.println(list.get(2));
    }

    public static void testSet(ArrayList<String> list) {
        System.out.println("==================");
        System.out.println(list.set(0, "6789"));
        System.out.println(list);
    }

    public static void testRemove(ArrayList<String> list) {

        System.out.println(list.remove("abc"));
        System.out.println(list.remove("zzz"));
        System.out.println(list.remove(0));
        System.out.println(list);
    }
    public static void RemoveWord(){
        ArrayList<String> list=new ArrayList<>();
        list.add("test");
        list.add("张三");
        list.add("test");
        list.add("李四");
        list.add("test");
        list.add("test");
        for (int i = 0; i < list.size(); i++) {
            if("test".equals(list.get(i))){
                list.remove(i);
                //删除元素，指针向前回退一格
                i--;
            }
        }
        System.out.println(list);
    }
}

